// package-owner/pages/owner-follow/index.js
import { TARGET_TYPE } from '../../../constants/common';
import { USER_ACTION_TYPE } from '../../../constants/user';
import { PageActionMenu, PageListMixins } from '../../../behaviors/page';
import { getUserFollow, getUserFollowBrand, getUserFollowStore, cancelFollowUser, cancelFollowBrand, cancelFollowStore } from '../../../api/usr';
import { dealFollowListData } from '../../../biztools/user';

Page({
    mixins: [PageActionMenu, PageListMixins],
    data: {
        tabsData: [{
            value: TARGET_TYPE.brand,
            title: '品牌',
        }, {
            value: TARGET_TYPE.store,
            title: '店铺',
        }, {
            value: TARGET_TYPE.user,
            title: '用户',
        }],
        searchValue: '',
        activeIndex: TARGET_TYPE.brand,
        actionFollowId: null,
        followList: [],
        moreActionMenu: [{
            name: '取消关注',
            action: USER_ACTION_TYPE.cancelFollowUser,
        }],
    },
    // 获取数据方法
    getListData: async function (isReplace=false) {
        const { pageNo, pageSize, pageTime, searchValue, followList, activeIndex } = this.data;
        const params = {
            pageNo,
            pageSize,
            ts: pageTime,
            keyword: searchValue,
        };
        let list = [];
        if (activeIndex === TARGET_TYPE.brand) {
            list = await getUserFollowBrand(params);
        } else if (activeIndex === TARGET_TYPE.store) {
            list = await getUserFollowStore(params);
        } else if (activeIndex === TARGET_TYPE.user) {
            list = await getUserFollow(params);
        }
        this.setData({
            followList: isReplace ? dealFollowListData(list) : [...followList, ...dealFollowListData(list)],
        });
    },
    // 切换分类
    onTabChange: function(event) {
        const { name } = event.detail;
        console.log('OwnerFollow => 切换类型', name);
        this.setData({
            pageNo: 1,
            pageTime: new Date().getTime(),
            activeIndex: name,
        }, async () => {
            await this.getListData(true);
        });
    },
    // 搜索用户
    searchFollow: function (event) {
        console.log('OwnerFollow => 搜索的内容为：', event.detail);
        this.setData({
            pageNo: 1,
            searchValue: event.detail,
        }, async () => {
            await this.getListData(true);
        });
    },
    // 更多操作
    moreFansAction: function (event) {
        console.log('OwnerFollow => 关注对象操作：', event.detail);
        this.setData({
            actionFollowId: event.detail,
        });
        this.showPageAction();
    },
    // 选择结果
    onActionSelect: async function (event) {
        console.log('OwnerFollow => 操作的类型为：', event.detail);
        const { actionFollowId, activeIndex } = this.data;
        if (event.detail.action === USER_ACTION_TYPE.cancelFollowUser) {
            // 执行取消关注
            let res;
            if (activeIndex === TARGET_TYPE.brand) {
                res = await cancelFollowBrand(actionFollowId);
            } else if (activeIndex === TARGET_TYPE.store) {
                res = await cancelFollowStore(actionFollowId);
            } else if (activeIndex === TARGET_TYPE.user) {
                res = await cancelFollowUser(actionFollowId);
            }
            if (res) {
                tipSuccess('取消关注成功');
                this.setData({
                    pageNo: 1,
                    pageSize: list.length - 1,
                }, async () => {
                    await this.getListData(true);
                });
            }
        }
    },
})